package com.morehair.controller.admin;

import com.morehair.bean.bo.admin.TopicReq;
import com.morehair.bean.pojo.Topic;
import com.morehair.bean.vo.admin.BaseData;
import com.morehair.bean.vo.admin.BaseRespVo;
import com.morehair.service.admin.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: DLX
 * @Date: 2021/6/6 20:35
 * @Description:
 **/

@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequestMapping("list")
    @RequiresPermissions("admin:topic:list")
    public BaseRespVo topicList(TopicReq topicReq) {

        BaseData<Topic> topicData = topicService.query(topicReq);
        return BaseRespVo.ok(topicData);
    }

    @RequestMapping("create")
    @RequiresPermissions("admin:topic:create")
    public BaseRespVo topicCreate(@RequestBody Topic topic) {
        Topic newTopic = topicService.topicCreate(topic);
        return BaseRespVo.ok(newTopic);
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:topic:update")
    public BaseRespVo topicUpdate(@RequestBody Topic topic) {
        Topic updatedTopic = topicService.topicUpdate(topic);
        if (updatedTopic == null) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(updatedTopic);
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:topic:delete")
    public BaseRespVo topicDelete(@RequestBody Topic topic) {
        int code = topicService.topicDelete(topic);
        if (code == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

}
